HTMLify
Left View of Binary Tree.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | // Left View of Binary Tree java solutions class Tree { //Function to return list containing elements of left view of binary tree. ArrayList<Integer> leftView(Node root) { // Your code here ArrayList<Integer> ans=new ArrayList<>(); Queue<Node> q=new LinkedList<>(); if(root==null){ return ans; }else{ q.add(root); } while(q.size() != 0){ int sze=q.size(); for(int i=1 ; i<=sze ;i++){ Node p=q.remove(); if(i==1){ ans.add(p.data); } if(p.left!=null){ q.add(p.left); } if(p.right!=null){ q.add(p.right); } } } return ans; } } |